package com.eshopkeeper.core.util.yunzhi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Response工具类
 * @author chen
 *
 */
public class ResponseUtil {

    public static final Logger logger = LogManager.getLogger(ResponseUtil.class);

    public static PrintWriter getTextWriter(HttpServletResponse response) throws IOException {
        response.setContentType("text/plain; charset=UTF-8");
        return response.getWriter();
    }

    public static PrintWriter getJsonOut(HttpServletResponse response) throws IOException {
        response.setContentType("text/json; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, Cookie");
        return response.getWriter();
    }

    public static void write(HttpServletResponse response, ResultDTO resultDTO) {
        try {
            PrintWriter out = getTextWriter(response);
            String json = resultDTO.toJsonString();
            logger.info("response-" + json);
            out.print(encode(json));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, String value) {
        try {
            PrintWriter out = getTextWriter(response);
            logger.info("response-" + value);
            out.print(encode(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeNotEncode(HttpServletResponse response, String value) {
        try {
            PrintWriter out = getTextWriter(response);
            logger.info("response-" + value);
            out.print(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, JSONObject value) {
        try {
            PrintWriter out = getJsonOut(response);
            String valueStr = value.toString();
            logger.info("response-" + valueStr);
            out.print(encode(valueStr));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, JSONArray value) {
        try {
            PrintWriter out = getJsonOut(response);
            String valueStr = value.toString();
            logger.info("response-" + valueStr);
            out.print(encode(value.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String encode(String result) {
        try {
            return DES3Util.encode(result);
        } catch (Exception e) {
            logger.error("加密失败", e);
        }
        return "";
    }
}
